<template>
  <view class="container">
    <view class="header">
      <view class="title">好友请求</view>
    </view>
    
    <view class="request-list" v-if="friendRequests.length > 0">
      <view class="request-item" v-for="request in friendRequests" :key="request.id">
        <image class="avatar" :src="request.avatarUrl || '/static/avater/default.png'" />
        <view class="request-info">
          <view class="username">{{ request.username }}</view>
          <view class="request-time">{{ formatTime(request.created_at) }}</view>
        </view>
        <view class="request-actions">
          <button class="accept-btn" @click="handleRequest(request.id, 'accept')">接受</button>
          <button class="reject-btn" @click="handleRequest(request.id, 'reject')">拒绝</button>
        </view>
      </view>
    </view>
    
    <view class="empty-state" v-else>
      <image src="/static/icon/empty.png" class="empty-icon" />
      <text class="empty-text">暂无好友请求</text>
    </view>
  </view>
</template>

<script>
import request from '../../utils/request.js';

// 在onLoad或onShow中添加全局事件监听
export default {
  data() {
    return {
      friendRequests: [],
      loading: false
    };
  },
  onLoad() {
    this.loadFriendRequests();
    
    // 添加全局事件监听
    this.friendRequestListener = uni.$on('newFriendRequestReceived', (data) => {
      console.log('收到新好友请求:', data);
      // 立即刷新好友请求列表
      this.loadFriendRequests();
    });
  },
  onUnload() {
    // 恢复正确的逻辑顺序和条件检查
    if (this.friendRequestListener) {
      uni.$off('newFriendRequestReceived', this.friendRequestListener);
    }
  },
  methods: {
    // 加载好友请求列表
    async loadFriendRequests() {
      try {
        const res = await request({
          url: '/auth/friend-requests',
          method: 'GET'
        });
        this.friendRequests = res.data || [];
      } catch (error) {
        console.error('加载好友请求失败:', error);
        uni.showToast({ title: '加载失败', icon: 'none' });
        this.friendRequests = [];
      }
    },
    
    // 处理好友请求
    async handleRequest(requestId, action) {
      console.log('处理好友请求:', requestId, action);
      try {
        const res = await request({
          url: `/auth/friend-requests/${requestId}/handle`,
          method: 'POST',
          data: { action }
        });
        
        uni.showToast({ title: res.message || '操作成功', icon: 'success' });
        // 重新加载好友请求列表
        this.loadFriendRequests();
        
        // 通知联系人页面更新好友列表
        uni.$emit('refreshFriends');
      } catch (error) {
        console.error('处理好友请求失败:', error);
        uni.showToast({ title: '处理失败', icon: 'none' });
      }
    },
    
    // 格式化时间
    formatTime(timeString) {
      const date = new Date(timeString);
      const now = new Date();
      const diff = now - date;
      
      // 小于1分钟
      if (diff < 60000) {
        return '刚刚';
      }
      // 小于1小时
      else if (diff < 3600000) {
        return Math.floor(diff / 60000) + '分钟前';
      }
      // 小于24小时
      else if (diff < 86400000) {
        return Math.floor(diff / 3600000) + '小时前';
      }
      // 小于30天
      else if (diff < 2592000000) {
        return Math.floor(diff / 86400000) + '天前';
      }
      // 更早的时间
      else {
        return date.getFullYear() + '-' + 
               String(date.getMonth() + 1).padStart(2, '0') + '-' + 
               String(date.getDate()).padStart(2, '0');
      }
    }
  }
};
</script>

<style scoped>
.container {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.header {
  padding: 16px;
  background-color: #fff;
  border-bottom: 1px solid #f2f2f2;
}

.title {
  font-size: 18px;
  font-weight: 500;
  color: #222;
  text-align: center;
}

.request-list {
  background-color: #fff;
  margin-top: 12px;
}

.request-item {
  display: flex;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid #f2f2f2;
}

.avatar {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  margin-right: 16px;
}

.request-info {
  flex: 1;
}

.username {
  font-size: 16px;
  color: #222;
  margin-bottom: 4px;
}

.request-time {
  font-size: 13px;
  color: #888;
}

.request-actions {
  display: flex;
  gap: 12px;
}

.accept-btn {
  background-color: #1aad19;
  color: #fff;
  border: none;
  border-radius: 4px;
  padding: 8px 16px;
  font-size: 14px;
}

.reject-btn {
  background-color: #f5f5f5;
  color: #888;
  border: none;
  border-radius: 4px;
  padding: 8px 16px;
  font-size: 14px;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
}

.empty-icon {
  width: 80px;
  height: 80px;
  opacity: 0.4;
  margin-bottom: 16px;
}

.empty-text {
  font-size: 15px;
  color: #888;
}
</style>